#include <pthread.h>
#include <cstdio>
#include <cstring>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <cstdlib>

#define PROT 1234
#define BACKLOG 5
#define MAXDATASIZE 100

void process_cli(int connfd, struct sockaddr_in client);

void* function(void* arg);

struct ARG{
    int connfd;
    struct sockaddr_in client;
};

int main(){
    int listenfd, connfd;
    pthread_t tid;
    struct sockaddr_in server, client;
    struct ARG* arg;

    if((listenfd = socket(AF_INET, SOCK_STREAM, 0)) == -1){
        perror("Create socket failed.\n");
        exit(1);
    }
    
    int opt = SO_REUSEADDR;
    setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt));

    bzero(&server, sizeof(server));
    server.sin_family = AF_INET;
    server.sin_port = htons(PROT);
    server.sin_addr.s_addr = htonl(INADDR_ANY);

    if(bind(listenfd, (struct sockaddr*) &server, sizeof(server)) == -1){
        perror("bind error");
        exit(1);
    }

    if(listen(listenfd, BACKLOG) == -1){
        perror("listen error.\n");
        exit(1);
    }

    socklen_t  len = sizeof(client);
    while(1){
        if((connfd = accept(listenfd, (struct sockaddr*) &client, &len)) == -1){
            perror("accept error.\n");
            exit(1);
        }

        // 建立连接后，将相关参数填充至arg
        arg = (struct ARG*)malloc(sizeof(struct ARG));
        arg->connfd = connfd;
        memcpy((void*) &arg->client, &client, sizeof(client));

        // 创建线程
        if(pthread_create(&tid, NULL, function, (void*) arg)){
            perror("Pthread_create error.\n");
            exit(1);
        }
    }

    close(listenfd);
       
    return 0;
}

void process_cli(int connfd, struct sockaddr_in client){
    int num;
    char recvbuf[MAXDATASIZE], sendbuf[MAXDATASIZE], cli_name[MAXDATASIZE];
    printf("You got a connection from %s.\n", inet_ntoa(client.sin_addr));

    // 接收客户端发来的名字
    if(num = recv(connfd, cli_name, MAXDATASIZE, 0) == 0){
        close(connfd);
        printf("Client disconnected.\n");
        return;
    }

    cli_name[num - 1] = '\0';
    printf("Client's name is %s.\n", cli_name);
    // 接收客户端发送的数据
    while(num = recv(connfd, recvbuf, MAXDATASIZE, 0)){
        recvbuf[num] = '\0';
        printf("Received client(%s) message: %s", cli_name, recvbuf);

        // 处理客户端发送的数据
        int i = 0;
        for(i = 0;i < num - 1;i++){
            if((recvbuf[i] >= 'a' && recvbuf[i] <= 'z') || (recvbuf[i] >= 'A' && recvbuf[i] <= 'Z')){
                recvbuf[i] += 3;
                if((recvbuf[i] > 'Z' && recvbuf[i] < 'Z' + 3) || (recvbuf[i] > 'z')){
                    recvbuf[i] -= 26;
                }
                
            }
            sendbuf[i] = recvbuf[i];
        }
        // 将处理后的数据发送给客户端
        sendbuf[num - 1] = '\0';
        send(connfd, sendbuf, strlen(sendbuf), 0);
    }

    close(connfd);
    
}

// 线程的入口函数
void* function(void* arg){
    printf("New thread created.tid:%ld\n", (long unsigned int) pthread_self());
    struct ARG* info = (struct ARG*) arg;
    process_cli(info->connfd, info->client);
    free (arg);
    printf("%l6d thread exit.\n", (long unsigned int) pthread_self());
    pthread_exit(NULL);
}